package Demo;

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

import org.apache.bcel.classfile.ClassParser;
import org.apache.bcel.classfile.JavaClass;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;
import java.util.zip.CRC32;

public class JarFileLoader {
    private JarFile jarfile;
    private ArrayList<JarEntry> entryList = new ArrayList();
    private ArrayList<JavaClass> orgclsList = new ArrayList();

    public JarFileLoader() {
    }

    public ArrayList<JavaClass> inputJar(String inputPath) {
        this.entryList.clear();
        this.orgclsList.clear();

        try {
            this.jarfile = new JarFile(inputPath);
            Enumeration entries = this.jarfile.entries();

            while(entries.hasMoreElements()) {
                JarEntry curEntry = (JarEntry)entries.nextElement();
                this.entryList.add(curEntry);
                if (curEntry.getName().endsWith(".class")) {
                    ClassParser clp = new ClassParser(inputPath, curEntry.getName());
                    JavaClass newClass = clp.parse();
                    this.orgclsList.add(newClass);
                } else {
                    this.orgclsList.add((JavaClass) null);
                }
            }
        } catch (IOException var6) {
            var6.printStackTrace();
            System.out.println("Failed to load the input file !");
        }

        return this.orgclsList;
    }

    public void outputJar(String outputPath, ArrayList<JavaClass> jclsList) {
        try {
            JarOutputStream jarops = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(outputPath, true)));
            CRC32 newCrc = new CRC32();

            for(int i = 0; i < this.entryList.size(); ++i) {
                JarEntry curEntry = (JarEntry)this.entryList.get(i);
                if (curEntry.getName().endsWith(".class")) {
                    JavaClass jcls = (JavaClass)jclsList.get(i);
                    byte[] jclassByte = jcls.getBytes();
                    curEntry.setMethod(0);
                    curEntry.setSize((long)jclassByte.length);
                    curEntry.setCompressedSize((long)jclassByte.length);
                    newCrc.reset();
                    newCrc.update(jclassByte);
                    curEntry.setCrc(newCrc.getValue());
                    jarops.putNextEntry(curEntry);
                    jarops.write(jclassByte);
                } else {
                    jarops.putNextEntry(curEntry);
                    InputStream is = this.jarfile.getInputStream(curEntry);
                    byte[] buffer = new byte[1024];

                    int byteRead;
                    while((byteRead = is.read(buffer)) != -1) {
                        jarops.write(buffer, 0, byteRead);
                    }

                    is.close();
                }

                jarops.closeEntry();
            }

            this.jarfile.close();
            jarops.close();
        } catch (IOException var12) {
            var12.printStackTrace();
            System.out.println("Failed to output the jar file !\n" + var12.getMessage());
        }

    }

    public ArrayList<JarEntry> getEntries() {
        return this.entryList;
    }

    public ArrayList<JavaClass> getClsList() {
        return this.orgclsList;
    }
}
